{
 "metadata": {
  "name": "",
  "signature": "sha256:067c1802b971ba842da856771eeac996386d181a92c8e25ea838928b63b2ecdc"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Appendix: Python Language Essentials"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import division\n",
      "from numpy.random import randn\n",
      "import numpy as np\n",
      "import os\n",
      "import matplotlib.pyplot as plt\n",
      "np.random.seed(12345)\n",
      "plt.rc('figure', figsize=(10, 6))\n",
      "from pandas import *\n",
      "import pandas\n",
      "np.set_printoptions(precision=4)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "The Python interpreter"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "```\n",
      "$ python\n",
      "Python 2.7.2 (default, Oct  4 2011, 20:06:09)\n",
      "[GCC 4.6.1] on linux2\n",
      "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
      ">>> a = 5\n",
      ">>> print a\n",
      "5\n",
      "```"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%%writefile hello_world.py\n",
      "print 'Hello world'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "```\n",
      "$ ipython\n",
      "Python 2.7.2 |EPD 7.1-2 (64-bit)| (default, Jul  3 2011, 15:17:51)\n",
      "Type \"copyright\", \"credits\" or \"license\" for more information.\n",
      "\n",
      "IPython 0.12 -- An enhanced Interactive Python.\n",
      "?         -> Introduction and overview of IPython's features.\n",
      "%quickref -> Quick reference.\n",
      "help      -> Python's own help system.\n",
      "object?   -> Details about 'object', use 'object??' for extra details.\n",
      "\n",
      "In [1]: %run hello_world.py\n",
      "Hello world\n",
      "\n",
      "In [2]:\n",
      "```"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "The Basics"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Language Semantics"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Indentation, not braces"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "for x in array:\n",
      "    if x < pivot:\n",
      "        less.append(x)\n",
      "    else:\n",
      "        greater.append(x)"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "for x in array {\n",
      "        if x < pivot {\n",
      "            less.append(x)\n",
      "        } else {\n",
      "            greater.append(x)\n",
      "        }\n",
      "    }"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "for x in array\n",
      "    {\n",
      "      if x < pivot\n",
      "      {\n",
      "        less.append(x)\n",
      "      }\n",
      "      else\n",
      "      {\n",
      "        greater.append(x)\n",
      "      }\n",
      "    }"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "a = 5; b = 6; c = 7"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Everything is an object"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Comments"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "results = []\n",
      "for line in file_handle:\n",
      "    # keep the empty lines for now\n",
      "    # if len(line) == 0:\n",
      "    #   continue\n",
      "    results.append(line.replace('foo', 'bar'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Function and object method calls"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "result = f(x, y, z)\n",
      "g()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "obj.some_method(x, y, z)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "result = f(a, b, c, d=5, e='foo')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Variables and pass-by-reference"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = [1, 2, 3]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b = a"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a.append(4)\n",
      "b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def append_element(some_list, element):\n",
      "    some_list.append(element)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = [1, 2, 3]\n",
      "\n",
      "append_element(data, 4)\n",
      "\n",
      "In [4]: data\n",
      "Out[4]: [1, 2, 3, 4]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Dynamic references, strong types"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 5\n",
      "type(a)\n",
      "a = 'foo'\n",
      "type(a)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'5' + 5"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 4.5\n",
      "b = 2\n",
      "# String formatting, to be visited later\n",
      "print 'a is %s, b is %s' % (type(a), type(b))\n",
      "a / b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 5\n",
      "isinstance(a, int)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 5; b = 4.5\n",
      "isinstance(a, (int, float))\n",
      "isinstance(b, (int, float))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Attributes and methods"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "In [1]: a = 'foo'\n",
      "\n",
      "In [2]: a.<Tab>\n",
      "a.capitalize  a.format      a.isupper     a.rindex      a.strip\n",
      "a.center      a.index       a.join        a.rjust       a.swapcase\n",
      "a.count       a.isalnum     a.ljust       a.rpartition  a.title\n",
      "a.decode      a.isalpha     a.lower       a.rsplit      a.translate\n",
      "a.encode      a.isdigit     a.lstrip      a.rstrip      a.upper\n",
      "a.endswith    a.islower     a.partition   a.split       a.zfill\n",
      "a.expandtabs  a.isspace     a.replace     a.splitlines\n",
      "a.find        a.istitle     a.rfind       a.startswith"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      ">>> getattr(a, 'split')\n",
      "<function split>\n"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "\"Duck\" typing"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def isiterable(obj):\n",
      "    try:\n",
      "        iter(obj)\n",
      "        return True\n",
      "    except TypeError: # not iterable\n",
      "        return False"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "isiterable('a string')\n",
      "isiterable([1, 2, 3])\n",
      "isiterable(5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "if not isinstance(x, list) and isiterable(x):\n",
      "    x = list(x)"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Imports"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# some_module.py\n",
      "PI = 3.14159\n",
      "\n",
      "def f(x):\n",
      "    return x + 2\n",
      "\n",
      "def g(a, b):\n",
      "    return a + b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import some_module\n",
      "result = some_module.f(5)\n",
      "pi = some_module.PI"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from some_module import f, g, PI\n",
      "result = g(5, PI)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import some_module as sm\n",
      "from some_module import PI as pi, g as gf\n",
      "\n",
      "r1 = sm.f(pi)\n",
      "r2 = gf(6, pi)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Binary operators and comparisons"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "5 - 7\n",
      "12 + 21.5\n",
      "5 <= 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = [1, 2, 3]\n",
      "b = a\n",
      "# Note, the list function always creates a new list\n",
      "c = list(a)\n",
      "a is b\n",
      "a is not c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a == c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = None\n",
      "a is None"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Strictness versus laziness"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = b = c = 5\n",
      "d = a + b * c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Mutable and immutable objects"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a_list = ['foo', 2, [4, 5]]\n",
      "a_list[2] = (3, 4)\n",
      "a_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a_tuple = (3, 5, (4, 5))\n",
      "a_tuple[1] = 'four'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Scalar Types"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Numeric types"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ival = 17239871\n",
      "ival ** 6"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fval = 7.243\n",
      "fval2 = 6.78e-5"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "3 / 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import division"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "3 / float(2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "3 // 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cval = 1 + 2j\n",
      "cval * (1 - 2j)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Strings"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 'one way of writing a string'\n",
      "b = \"another way\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "c = \"\"\"\n",
      "This is a longer string that\n",
      "spans multiple lines\n",
      "\"\"\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 'this is a string'\n",
      "a[10] = 'f'\n",
      "b = a.replace('string', 'longer string')\n",
      "b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 5.6\n",
      "s = str(a)\n",
      "s"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "s = 'python'\n",
      "list(s)\n",
      "s[:3]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "s = '12\\\\34'\n",
      "print s"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "s = r'this\\has\\no\\special\\characters'\n",
      "s"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 'this is the first half '\n",
      "b = 'and this is the second half'\n",
      "a + b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "template = '%.2f %s are worth $%d'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "template % (4.5560, 'Argentine Pesos', 1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Booleans"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "True and True\n",
      "False or True"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = [1, 2, 3]\n",
      "if a:\n",
      "    print 'I found something!'\n",
      "\n",
      "b = []\n",
      "if not b:\n",
      "    print 'Empty!'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "bool([]), bool([1, 2, 3])\n",
      "bool('Hello world!'), bool('')\n",
      "bool(0), bool(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Type casting"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "s = '3.14159'\n",
      "fval = float(s)\n",
      "type(fval)\n",
      "int(fval)\n",
      "bool(fval)\n",
      "bool(0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "None"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = None\n",
      "a is None\n",
      "b = 5\n",
      "b is not None"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def add_and_maybe_multiply(a, b, c=None):\n",
      "    result = a + b\n",
      "\n",
      "    if c is not None:\n",
      "        result = result * c\n",
      "\n",
      "    return result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Dates and times"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from datetime import datetime, date, time\n",
      "dt = datetime(2011, 10, 29, 20, 30, 21)\n",
      "dt.day\n",
      "dt.minute"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dt.date()\n",
      "dt.time()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dt.strftime('%m/%d/%Y %H:%M')\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "datetime.strptime('20091031', '%Y%m%d')\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dt.replace(minute=0, second=0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dt2 = datetime(2011, 11, 15, 22, 30)\n",
      "delta = dt2 - dt\n",
      "delta\n",
      "type(delta)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dt\n",
      "dt + delta"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Control Flow"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "If, elif, and else"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "if x < 0:\n",
      "    print 'It's negative'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "if x < 0:\n",
      "    print 'It's negative'\n",
      "elif x == 0:\n",
      "    print 'Equal to zero'\n",
      "elif 0 < x < 5:\n",
      "    print 'Positive but smaller than 5'\n",
      "else:\n",
      "    print 'Positive and larger than 5'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = 5; b = 7\n",
      "c = 8; d = 4\n",
      "if a < b or c > d:\n",
      "    print 'Made it'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "For loops"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for value in collection:\n",
      "    # do something with value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = [1, 2, None, 4, None, 5]\n",
      "total = 0\n",
      "for value in sequence:\n",
      "    if value is None:\n",
      "        continue\n",
      "    total += value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sequence = [1, 2, 0, 4, 6, 5, 2, 1]\n",
      "total_until_5 = 0\n",
      "for value in sequence:\n",
      "    if value == 5:\n",
      "        break\n",
      "    total_until_5 += value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for a, b, c in iterator:\n",
      "    # do something"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "While loops"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = 256\n",
      "total = 0\n",
      "while x > 0:\n",
      "    if total > 500:\n",
      "        break\n",
      "    total += x\n",
      "    x = x // 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "pass"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "if x < 0:\n",
      "    print 'negative!'\n",
      "elif x == 0:\n",
      "    # TODO: put something smart here\n",
      "    pass\n",
      "else:\n",
      "    print 'positive!'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def f(x, y, z):\n",
      "    # TODO: implement this function!\n",
      "    pass\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Exception handling"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "float('1.2345')\n",
      "float('something')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def attempt_float(x):\n",
      "    try:\n",
      "        return float(x)\n",
      "    except:\n",
      "        return x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "attempt_float('1.2345')\n",
      "attempt_float('something')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "float((1, 2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def attempt_float(x):\n",
      "    try:\n",
      "        return float(x)\n",
      "    except ValueError:\n",
      "        return x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "attempt_float((1, 2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def attempt_float(x):\n",
      "    try:\n",
      "        return float(x)\n",
      "    except (TypeError, ValueError):\n",
      "        return x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "f = open(path, 'w')\n",
      "\n",
      "try:\n",
      "    write_to_file(f)\n",
      "finally:\n",
      "    f.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "f = open(path, 'w')\n",
      "\n",
      "try:\n",
      "    write_to_file(f)\n",
      "except:\n",
      "    print 'Failed'\n",
      "else:\n",
      "    print 'Succeeded'\n",
      "finally:\n",
      "    f.close()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "range and xrange"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "range(10)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "range(0, 20, 2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq = [1, 2, 3, 4]\n",
      "for i in range(len(seq)):\n",
      "    val = seq[i]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sum = 0\n",
      "for i in xrange(10000):\n",
      "    # % is the modulo operator\n",
      "    if i % 3 == 0 or i % 5 == 0:\n",
      "        sum += i\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Ternary Expressions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = 5\n",
      "value = 'Non-negative' if x >= 0 else 'Negative'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Data structures and sequences"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Tuple"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tup = 4, 5, 6\n",
      "tup"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nested_tup = (4, 5, 6), (7, 8)\n",
      "nested_tup"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tuple([4, 0, 2])\n",
      "tup = tuple('string')\n",
      "tup"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tup[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tup = tuple(['foo', [1, 2], True])\n",
      "tup[2] = False\n",
      "\n",
      "# however\n",
      "tup[1].append(3)\n",
      "tup"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "(4, None, 'foo') + (6, 0) + ('bar',)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "('foo', 'bar') * 4"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Unpacking tuples"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tup = (4, 5, 6)\n",
      "a, b, c = tup\n",
      "b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "tup = 4, 5, (6, 7)\n",
      "a, b, (c, d) = tup\n",
      "d"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "tmp = a\n",
      "a = b\n",
      "b = tmp"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "b, a = a, b"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "\n",
      "seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]\n",
      "for a, b, c in seq:\n",
      "    pass"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Tuple methods"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = (1, 2, 2, 2, 3, 4, 2)\n",
      "a.count(2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "List"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a_list = [2, 3, 7, None]\n",
      "\n",
      "tup = ('foo', 'bar', 'baz')\n",
      "b_list = list(tup)\n",
      "b_list\n",
      "b_list[1] = 'peekaboo'\n",
      "b_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Adding and removing elements"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b_list.append('dwarf')\n",
      "b_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b_list.insert(1, 'red')\n",
      "b_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b_list.pop(2)\n",
      "b_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b_list.append('foo')\n",
      "b_list.remove('foo')\n",
      "b_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'dwarf' in b_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Concatenating and combining lists"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "[4, None, 'foo'] + [7, 8, (2, 3)]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = [4, None, 'foo']\n",
      "x.extend([7, 8, (2, 3)])\n",
      "x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "everything = []\n",
      "for chunk in list_of_lists:\n",
      "    everything.extend(chunk)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "everything = []\n",
      "for chunk in list_of_lists:\n",
      "    everything = everything + chunk"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Sorting"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = [7, 2, 5, 1, 3]\n",
      "a.sort()\n",
      "a"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "b = ['saw', 'small', 'He', 'foxes', 'six']\n",
      "b.sort(key=len)\n",
      "b"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Binary search and maintaining a sorted list"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import bisect\n",
      "c = [1, 2, 2, 2, 3, 4, 7]\n",
      "bisect.bisect(c, 2)\n",
      "bisect.bisect(c, 5)\n",
      "bisect.insort(c, 6)\n",
      "c"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Slicing"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq = [7, 2, 3, 7, 5, 6, 0, 1]\n",
      "seq[1:5]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq[3:4] = [6, 3]\n",
      "seq"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq[:5]\n",
      "seq[3:]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq[-4:]\n",
      "seq[-6:-2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq[::2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq[::-1]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Built-in Sequence Functions"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "enumerate"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "i = 0\n",
      "for value in collection:\n",
      "   # do something with value\n",
      "   i += 1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for i, value in enumerate(collection):\n",
      "   # do something with value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "some_list = ['foo', 'bar', 'baz']\n",
      "mapping = dict((v, i) for i, v in enumerate(some_list))\n",
      "mapping"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "sorted"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sorted([7, 1, 2, 6, 0, 3, 2])\n",
      "sorted('horse race')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sorted(set('this is just some string'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "zip"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq1 = ['foo', 'bar', 'baz']\n",
      "seq2 = ['one', 'two', 'three']\n",
      "zip(seq1, seq2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "seq3 = [False, True]\n",
      "zip(seq1, seq2, seq3)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for i, (a, b) in enumerate(zip(seq1, seq2)):\n",
      "    print('%d: %s, %s' % (i, a, b))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pitchers = [('Nolan', 'Ryan'), ('Roger', 'Clemens'),\n",
      "            ('Schilling', 'Curt')]\n",
      "first_names, last_names = zip(*pitchers)\n",
      "first_names\n",
      "last_names"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "zip(seq[0], seq[1], ..., seq[len(seq) - 1])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "reversed"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "list(reversed(range(10)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Dict"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "empty_dict = {}\n",
      "d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}\n",
      "d1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "d1[7] = 'an integer'\n",
      "d1\n",
      "d1['b']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "'b' in d1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "d1[5] = 'some value'\n",
      "d1['dummy'] = 'another value'\n",
      "del d1[5]\n",
      "ret = d1.pop('dummy')\n",
      "ret"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "d1.keys()\n",
      "d1.values()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "d1.update({'b' : 'foo', 'c' : 12})\n",
      "d1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Creating dicts from sequences"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mapping = {}\n",
      "for key, value in zip(key_list, value_list):\n",
      "    mapping[key] = value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mapping = dict(zip(range(5), reversed(range(5))))\n",
      "mapping"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Default values"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "if key in some_dict:\n",
      "    value = some_dict[key]\n",
      "else:\n",
      "    value = default_value"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "value = some_dict.get(key, default_value)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "words = ['apple', 'bat', 'bar', 'atom', 'book']\n",
      "by_letter = {}\n",
      "\n",
      "for word in words:\n",
      "    letter = word[0]\n",
      "    if letter not in by_letter:\n",
      "        by_letter[letter] = [word]\n",
      "    else:\n",
      "        by_letter[letter].append(word)\n",
      "\n",
      "by_letter"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "by_letter.setdefault(letter, []).append(word)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from collections import defaultdict\n",
      "by_letter = defaultdict(list)\n",
      "for word in words:\n",
      "    by_letter[word[0]].append(word)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "counts = defaultdict(lambda: 4)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Valid dict key types"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "hash('string')\n",
      "hash((1, 2, (2, 3)))\n",
      "hash((1, 2, [2, 3])) # fails because lists are mutable"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "d = {}\n",
      "d[tuple([1, 2, 3])] = 5\n",
      "d"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Set"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "set([2, 2, 2, 1, 3, 3])\n",
      "{2, 2, 2, 1, 3, 3}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = {1, 2, 3, 4, 5}\n",
      "b = {3, 4, 5, 6, 7, 8}\n",
      "a | b  # union (or)\n",
      "a & b  # intersection (and)\n",
      "a - b  # difference\n",
      "a ^ b  # symmetric difference (xor)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a_set = {1, 2, 3, 4, 5}\n",
      "{1, 2, 3}.issubset(a_set)\n",
      "a_set.issuperset({1, 2, 3})"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "{1, 2, 3} == {3, 2, 1}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "List, set, and dict comprehensions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "strings = ['a', 'as', 'bat', 'car', 'dove', 'python']\n",
      "[x.upper() for x in strings if len(x) > 2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "unique_lengths = {len(x) for x in strings}\n",
      "unique_lengths"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "loc_mapping = {val : index for index, val in enumerate(strings)}\n",
      "loc_mapping"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "loc_mapping = dict((val, idx) for idx, val in enumerate(strings)}"
     ]
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Nested list comprehensions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "all_data = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'],\n",
      "            ['Susie', 'Casey', 'Jill', 'Ana', 'Eva', 'Jennifer', 'Stephanie']]\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "names_of_interest = []\n",
      "for names in all_data:\n",
      "    enough_es = [name for name in names if name.count('e') > 2]\n",
      "    names_of_interest.extend(enough_es)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "result = [name for names in all_data for name in names\n",
      "          if name.count('e') >= 2]\n",
      "result\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "some_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]\n",
      "flattened = [x for tup in some_tuples for x in tup]\n",
      "flattened"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "flattened = []\n",
      "\n",
      "for tup in some_tuples:\n",
      "    for x in tup:\n",
      "        flattened.append(x)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [229]: [[x for x in tup] for tup in some_tuples]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Functions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def my_function(x, y, z=1.5):\n",
      "    if z > 1:\n",
      "        return z * (x + y)\n",
      "    else:\n",
      "        return z / (x + y)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "my_function(5, 6, z=0.7)\n",
      "my_function(3.14, 7, 3.5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Namespaces, scope, and local functions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def func():\n",
      "    a = []\n",
      "    for i in range(5):\n",
      "        a.append(i)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = []\n",
      "def func():\n",
      "    for i in range(5):\n",
      "        a.append(i)\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a = None\n",
      "def bind_a_variable():\n",
      "    global a\n",
      "    a = []\n",
      "bind_a_variable()\n",
      "print a"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def outer_function(x, y, z):\n",
      "    def inner_function(a, b, c):\n",
      "        pass\n",
      "    pass"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Returning multiple values"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def f():\n",
      "    a = 5\n",
      "    b = 6\n",
      "    c = 7\n",
      "    return a, b, c\n",
      "\n",
      "a, b, c = f()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "return_value = f()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def f():\n",
      "    a = 5\n",
      "    b = 6\n",
      "    c = 7\n",
      "    return {'a' : a, 'b' : b, 'c' : c}"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Functions are objects"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "states = ['   Alabama ', 'Georgia!', 'Georgia', 'georgia', 'FlOrIda',\n",
      "          'south   carolina##', 'West virginia?']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import re  # Regular expression module\n",
      "\n",
      "def clean_strings(strings):\n",
      "    result = []\n",
      "    for value in strings:\n",
      "        value = value.strip()\n",
      "        value = re.sub('[!#?]', '', value) # remove punctuation\n",
      "        value = value.title()\n",
      "        result.append(value)\n",
      "    return result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [15]: clean_strings(states)\n",
      "Out[15]:\n",
      "['Alabama',\n",
      " 'Georgia',\n",
      " 'Georgia',\n",
      " 'Georgia',\n",
      " 'Florida',\n",
      " 'South Carolina',\n",
      " 'West Virginia']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def remove_punctuation(value):\n",
      "    return re.sub('[!#?]', '', value)\n",
      "\n",
      "clean_ops = [str.strip, remove_punctuation, str.title]\n",
      "\n",
      "def clean_strings(strings, ops):\n",
      "    result = []\n",
      "    for value in strings:\n",
      "        for function in ops:\n",
      "            value = function(value)\n",
      "        result.append(value)\n",
      "    return result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [22]: clean_strings(states, clean_ops)\n",
      "Out[22]:\n",
      "['Alabama',\n",
      " 'Georgia',\n",
      " 'Georgia',\n",
      " 'Georgia',\n",
      " 'Florida',\n",
      " 'South Carolina',\n",
      " 'West Virginia']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [23]: map(remove_punctuation, states)\n",
      "Out[23]:\n",
      "['   Alabama ',\n",
      " 'Georgia',\n",
      " 'Georgia',\n",
      " 'georgia',\n",
      " 'FlOrIda',\n",
      " 'south   carolina',\n",
      " 'West virginia']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Anonymous (lambda) functions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def short_function(x):\n",
      "    return x * 2\n",
      "\n",
      "equiv_anon = lambda x: x * 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def apply_to_list(some_list, f):\n",
      "    return [f(x) for x in some_list]\n",
      "\n",
      "ints = [4, 0, 1, 5, 6]\n",
      "apply_to_list(ints, lambda x: x * 2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "strings = ['foo', 'card', 'bar', 'aaaa', 'abab']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "strings.sort(key=lambda x: len(set(list(x))))\n",
      "strings"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Closures: functions that return functions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def make_closure(a):\n",
      "    def closure():\n",
      "        print('I know the secret: %d' % a)\n",
      "    return closure\n",
      "\n",
      "closure = make_closure(5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def make_watcher():\n",
      "    have_seen = {}\n",
      "\n",
      "    def has_been_seen(x):\n",
      "        if x in have_seen:\n",
      "            return True\n",
      "        else:\n",
      "            have_seen[x] = True\n",
      "            return False\n",
      "\n",
      "    return has_been_seen"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "watcher = make_watcher()\n",
      "vals = [5, 6, 1, 5, 1, 6, 3, 5]\n",
      "[watcher(x) for x in vals]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "def make_counter():\n",
      "    count = [0]\n",
      "    def counter():\n",
      "        # increment and return the current count\n",
      "        count[0] += 1\n",
      "        return count[0]\n",
      "    return counter\n",
      "\n",
      "counter = make_counter()"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def format_and_pad(template, space):\n",
      "    def formatter(x):\n",
      "        return (template % x).rjust(space)\n",
      "\n",
      "    return formatter"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fmt = format_and_pad('%.4f', 15)\n",
      "fmt(1.756)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Extended call syntax with *args, **kwargs"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "a, b, c = args\n",
      "d = kwargs.get('d', d_default_value)\n",
      "e = kwargs.get('e', e_default_value)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def say_hello_then_call_f(f, *args, **kwargs):\n",
      "    print 'args is', args\n",
      "    print 'kwargs is', kwargs\n",
      "    print(\"Hello! Now I'm going to call %s\" % f)\n",
      "    return f(*args, **kwargs)\n",
      "\n",
      "def g(x, y, z=1):\n",
      "    return (x + y) / z"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [8]:  say_hello_then_call_f(g, 1, 2, z=5.)\n",
      "args is (1, 2)\n",
      "kwargs is {'z': 5.0}\n",
      "Hello! Now I'm going to call <function g at 0x2dd5cf8>\n",
      "Out[8]: 0.6"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Currying: partial argument application"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def add_numbers(x, y):\n",
      "    return x + y"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "add_five = lambda y: add_numbers(5, y)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from functools import partial\n",
      "add_five = partial(add_numbers, 5)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# compute 60-day moving average of time series x\n",
      "ma60 = lambda x: pandas.rolling_mean(x, 60)\n",
      "\n",
      "# Take the 60-day moving average of of all time series in data\n",
      "data.apply(ma60)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Generators"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "some_dict = {'a': 1, 'b': 2, 'c': 3}\n",
      "for key in some_dict:\n",
      "    print key,"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dict_iterator = iter(some_dict)\n",
      "dict_iterator"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "list(dict_iterator)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def squares(n=10):\n",
      "    for i in xrange(1, n + 1):\n",
      "        print 'Generating squares from 1 to %d' % (n ** 2)\n",
      "        yield i ** 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [2]: gen = squares()\n",
      "\n",
      "In [3]: gen\n",
      "Out[3]: <generator object squares at 0x34c8280>"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "In [4]: for x in gen:\n",
      "   ...:     print x,\n",
      "   ...:\n",
      "Generating squares from 0 to 100\n",
      "1 4 9 16 25 36 49 64 81 100"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def make_change(amount, coins=[1, 5, 10, 25], hand=None):\n",
      "    hand = [] if hand is None else hand\n",
      "    if amount == 0:\n",
      "        yield hand\n",
      "    for coin in coins:\n",
      "        # ensures we don't give too much change, and combinations are unique\n",
      "        if coin > amount or (len(hand) > 0 and hand[-1] < coin):\n",
      "            continue\n",
      "\n",
      "        for result in make_change(amount - coin, coins=coins,\n",
      "                                  hand=hand + [coin]):\n",
      "            yield result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for way in make_change(100, coins=[10, 25, 50]):\n",
      "    print way\n",
      "len(list(make_change(100)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Generator expresssions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "gen = (x ** 2 for x in xrange(100))\n",
      "gen"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def _make_gen():\n",
      "    for x in xrange(100):\n",
      "        yield x ** 2\n",
      "gen = _make_gen()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sum(x ** 2 for x in xrange(100))\n",
      "dict((i, i **2) for i in xrange(5))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "itertools module"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import itertools\n",
      "first_letter = lambda x: x[0]\n",
      "\n",
      "names = ['Alan', 'Adam', 'Wes', 'Will', 'Albert', 'Steven']\n",
      "\n",
      "for letter, names in itertools.groupby(names, first_letter):\n",
      "    print letter, list(names) # names is a generator"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Files and the operating system"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "path = 'ch13/segismundo.txt'\n",
      "f = open(path)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for line in f:\n",
      "    pass"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "lines = [x.rstrip() for x in open(path)]\n",
      "lines"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "with open('tmp.txt', 'w') as handle:\n",
      "    handle.writelines(x for x in open(path) if len(x) > 1)\n",
      "\n",
      "open('tmp.txt').readlines()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "os.remove('tmp.txt')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}
